var type = ''
$(document).ready(function(){
    if(global.communityContent){
        // console.log(global.communityContent)
        if(global.communityContent.type == '0'){
            //总后台
        }else if(global.communityContent.type == '2'){
            //社区后台
            $('.screen').css('display','none')
        }
        type = global.communityContent.type
    }
    var provinceTag = document.getElementById("province");
    
    for(var i=0; i<provinceList.length; i++){
        var province = provinceList[i];
        var provinceName = ''
        if(province.name  == '北京'||province.name  == '上海'||province.name  == '天津'||province.name  == '重庆'){
            provinceName = province.name + '市'
        }else{
            provinceName = province.name + '省'
        }
        provinceTag.add(new Option(provinceName,i));
    }

    var communityTxt = '',sheng = '',shi = '',qu = ''
    $('#searchBtn').on('click',function(){
        communityTxt = $('.search-box').val()
        sheng = $('#province').find('option:selected').text()=='请选择'?'':$('#province').find('option:selected').text()
        shi = $('#city').find('option:selected').text()=='请选择'?'':$('#city').find('option:selected').text()
        qu = $('#area').find('option:selected').text()=='请选择'?'':$('#area').find('option:selected').text()
        getUserList(userPageNo,showNum,communityTxt,sheng,shi,qu);
    })

    //获取社区列表
    var userPageNo = 1,showNum=10;
    function getUserList(p,showNum,communityTxt,sheng,shi,qu){
        console.log(type)
        if(type == '0'){
            getAjax(`${global.apiUrl}/Api/Alladmin/get_user`,function(res){
                console.log(res);
                if(res.list.length == 0){
                    $("#page").paging({
                        pageNo:0,
                        totalPage: 0, //总页数
                        totalSize: 0,
                        callback: function(num) {
                            getUserList(num,showNum,communityTxt,sheng,shi,qu)
                        }
                    });
                    var html = `
                            <tr>
                                <td colspan="7" align="center">暂无用户</td>
                            </tr>`
                    $('#userList').html(html);
                }else{
                    $("#page").paging({
                        pageNo:p,
                        totalPage: Math.ceil(res.count/showNum), //总页数
                        totalSize: res.count,
                        callback: function(num) {
                            getUserList(num,showNum,communityTxt,sheng,shi,qu)
                            userPageNo = num
                        }
                    });
                    var html = '';
                    var list = res.list;
                    for(var i in list){
                        html += `
                            <tr>
                                <td>${list[i].name}</td>
                                <td>${list[i].id}</td>
                                <td>${jsGetAge(list[i].birthday)}</td>
                                <td>${list[i].sex}</td>
                                <td>${list[i].phone}</td>
                                <td>${list[i].sheng},${list[i].shi},${list[i].qu}</td>
                                <td>${list[i].community_content.community}</td>
                            </tr>
                    `
                    }
                    $('#userList').html(html);
                }
            },{
                p:p,
                num:showNum,
                like: communityTxt,
                sheng: sheng,
                shi: shi,
                qu: qu
            })
        }else if(type == '2'){
            getAjax(`${global.apiUrl}/Api/Alladmin/get_user`,function(res){
                // console.log(res);
                if(res.list.length == 0){
                    $("#page").paging({
                        pageNo:0,
                        totalPage: 0, //总页数
                        totalSize: 0,
                        callback: function(num) {
                            getUserList(num,showNum,communityTxt,sheng,shi,qu)
                        }
                    });
                    var html = `
                            <tr>
                                <td colspan="7" align="center">暂无用户</td>
                            </tr>`
                    $('#userList').html(html);
                }else{
                    $("#page").paging({
                        pageNo:p,
                        totalPage: Math.ceil(res.count/showNum), //总页数
                        totalSize: res.count,
                        callback: function(num) {
                            getUserList(num,showNum,communityTxt,sheng,shi,qu)
                            userPageNo = num
                        }
                    });
                    var html = '';
                    var list = res.list;
                    for(var i in list){
                        html += `
                            <tr>
                                <td>${list[i].name}</td>
                                <td>${list[i].id}</td>
                                <td>${jsGetAge(list[i].birthday)}</td>
                                <td>${list[i].sex}</td>
                                <td>${list[i].phone}</td>
                                <td>${list[i].sheng},${list[i].shi},${list[i].qu}</td>
                                <td>${list[i].community_content.community}</td>
                            </tr>
                    `
                    }
                    $('#userList').html(html);
                }
            },{
                p:p,
                num:showNum,
                community_id: global.community_id
            })
        }
    }
    getUserList(userPageNo,showNum,communityTxt,sheng,shi,qu);

    $('#province').on('change',function(){
        var province = $(this).find('option:selected').text()
        $("#city").empty();
        $('#city').append('<option value="-1">请选择</option>')
        $("#area").empty();
        $('#area').append('<option value="-1">请选择</option>')
        for(var i=0; i<provinceList.length; i++){
            if(provinceList[i].name == province.substring(0, province.length - 1)){
                if(provinceList[i].cityList){
                    for(var j in provinceList[i].cityList){
                        $('#city').append('<option value="'+j+'">'+provinceList[i].cityList[j].name+'</option>')
                    }
                }
            }
        }
    })
    $('#city').on('change',function(){
        var province = $('#province').find('option:selected').text()
        var city = $(this).find('option:selected').text()
        $("#area").empty();
        $('#area').append('<option value="-1">请选择</option>')
        for(var i=0; i<provinceList.length; i++){
            if(provinceList[i].name == province.substring(0, province.length - 1)){
                if(provinceList[i].cityList){   
                    for(var j in provinceList[i].cityList){
                        if(provinceList[i].cityList[j].name == city){
                            if(provinceList[i].cityList[j].areaList){
                                for(var z in provinceList[i].cityList[j].areaList){
                                    $('#area').append('<option value="'+z+'">'+provinceList[i].cityList[j].areaList[z]+'</option>')
                                }
                            }     
                        }
                    }
                }
            }
        }
    })
    
})